import React, { Component } from "react";
import "./index.css";
export default class Footer extends Component {
  //选中所有的回调
  handelCheckAll = (event) => {
    //event.target.checked  是否全选和全不选
    this.props.checkAllTodo(event.target.checked);
  };
  //清除所有的todo
  handleClearAll = () => {
    this.props.clearAllTodo();
  };

  render() {
    const { todos } = this.props;
    //已完成的个数,使用reduce
    const doneCount = todos.reduce((pre, item) => pre + (item.done ? 1 : 0), 0);
    //总数
    const total = todos.length;
    return (
      <div className="todo-footer">
        <label>
          {/* defaultChecked只能第一次使用 */}
          <input
            type="checkbox"
            checked={doneCount === total && total != 0}
            onChange={this.handelCheckAll}
          />
        </label>
        <span>
          <span>已完成{doneCount}</span> / 全部{total}
        </span>
        <button onClick={this.handleClearAll} className="btn btn-danger">
          清除已完成任务
        </button>
      </div>
    );
  }
}
